﻿
// SeclearDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "Seclear.h"
#include "SeclearDlg.h"
#include "afxdialogex.h"
#include <conio.h>
#include"tlhelp32.h"
#include <iostream>
#include <fstream>

#include <iostream>
#include <String>
#include <vector>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace std;
// CSeclearDlg 对话框

void fillZero(char* lpPath, LPVOID lpParam)
{
	CSeclearDlg *pDlg = (CSeclearDlg *)lpParam;
	char szFind[MAX_PATH];
	char temp_file[MAX_PATH];
	WIN32_FIND_DATA FindFileData;
	strcpy(szFind, lpPath);
	strcpy(temp_file, lpPath);
	strcat(temp_file, "\\");
	strcat(szFind, "\\*.*");
	HANDLE hFind = ::FindFirstFile(szFind, &FindFileData);
	if (INVALID_HANDLE_VALUE == hFind)  return;
	while (true)
	{
		if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if (FindFileData.cFileName[0] != '.')
			{
				char szFile[MAX_PATH];
				strcpy(szFile, lpPath);
				strcat(szFile, "\\");
				strcat(szFile, (char*)(FindFileData.cFileName));
				fillZero(szFile, lpParam);
			}
		}
		else
		{
			strcat(temp_file, FindFileData.cFileName);
			pDlg->m_show_tis.SetWindowTextA(FindFileData.cFileName);
			FILE *fp;
			fp = fopen(temp_file, "r");
			fseek(fp, 0, SEEK_END);
			long m_FileLen = ftell(fp);  
			fclose(fp);
			fp = fopen(temp_file, "w+");
			char s[] = "0";
			for (int i = 0 ;i< m_FileLen;i++)
			{ 
				fputs(s,   fp);
			}  
			fclose(fp);
			memset(temp_file, 0, MAX_PATH);
			strcpy(temp_file, lpPath);
			strcat(temp_file, "\\");
		}
		if (!FindNextFile(hFind, &FindFileData))  break;
	}
	FindClose(hFind);
}

void delZero(char* lpPath, LPVOID lpParam)
{
	CSeclearDlg* pDlg = (CSeclearDlg*)lpParam;
	char szFind[MAX_PATH];
	char temp_file[MAX_PATH];
	WIN32_FIND_DATA FindFileData;
	strcpy(szFind, lpPath);
	strcpy(temp_file, lpPath);
	strcat(temp_file, "\\");
	strcat(szFind, "\\*.*");
	HANDLE hFind = ::FindFirstFile(szFind, &FindFileData);
	if (INVALID_HANDLE_VALUE == hFind)  return;
	while (true)
	{
		if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if (FindFileData.cFileName[0] != '.')
			{
				char szFile[MAX_PATH];
				strcpy(szFile, lpPath);
				strcat(szFile, "\\");
				strcat(szFile, (char*)(FindFileData.cFileName));
				delZero(szFile, lpParam);
			}
		}
		else
		{
			strcat(temp_file, FindFileData.cFileName);
			pDlg->m_show_tis.SetWindowTextA(FindFileData.cFileName);
			remove(temp_file);
			memset(temp_file, 0, MAX_PATH);
			strcpy(temp_file, lpPath);
			strcat(temp_file, "\\");
		}
		if (!FindNextFile(hFind, &FindFileData))  break;
	}
	FindClose(hFind);
}


/* 获取微信“我的文档”里的聊天路径 */
CString getSpecialFolder() {
	char m_lpszDefaultDir[MAX_PATH];
	char   szDocument[MAX_PATH] = { 0 }; 
	memset(m_lpszDefaultDir, 0, _MAX_PATH);
 
	LPITEMIDLIST pidl = NULL;
	SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl);

	if (pidl && SHGetPathFromIDList(pidl, szDocument))
	{
		char wechatfile[14] = "\\WeChat Files"; 
		strncat(szDocument, wechatfile, MAX_PATH);
		GetShortPathName(szDocument, m_lpszDefaultDir, MAX_PATH);
	}  
	return  m_lpszDefaultDir;
}


/* 获取微信注册表里的聊天路径 */
CString setPath() {
	CString path = getSpecialFolder();
	if (path.IsEmpty()) {
		HKEY hKEY;
		CString data_Set = "SOFTWARE\\Tencent\\WeChat";
		int ret = ::RegOpenKeyEx(HKEY_CURRENT_USER, data_Set, 0, KEY_READ, &hKEY);
		if (ERROR_SUCCESS == ret) {
			LPBYTE lpData = new BYTE[256];
			DWORD cbData = 256;
			DWORD dwType = REG_SZ;
			if (::RegQueryValueEx(hKEY, _T("FileSavePath"), 0, &dwType, lpData, &cbData) != ERROR_SUCCESS) {
				return "";
			}
			::RegCloseKey(hKEY); 
			char m_lpszDefaultDir[MAX_PATH]; 
			path = (LPCTSTR)lpData;
			if ("MyDocument:" == path) {
				path = getSpecialFolder();
			}
			else {
				GetShortPathName((LPCTSTR)lpData, m_lpszDefaultDir, MAX_PATH);
				path.Format("%s", m_lpszDefaultDir);
			}
		}
	}
	return path;
}


DWORD GetProcessidFromName(LPCTSTR name)
{
	PROCESSENTRY32 pe;
	DWORD id = 0;
	HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	pe.dwSize = sizeof(PROCESSENTRY32);
	if (!Process32First(hSnapshot, &pe))
		return 0;
	while (1)
	{
		pe.dwSize = sizeof(PROCESSENTRY32);
		if (Process32Next(hSnapshot, &pe) == FALSE)
			break;
		if (strcmp(pe.szExeFile, name) == 0)
		{ 
			id = pe.th32ProcessID;

			break;
		} 
	}
	CloseHandle(hSnapshot);
	return id;
}


CSeclearDlg::CSeclearDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_SECLEAR_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSeclearDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_STATIC1, m_Graph);
	DDX_Control(pDX, show_tip, m_show_tis);
}

BEGIN_MESSAGE_MAP(CSeclearDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CSeclearDlg::OnBnClickedButton1)
	ON_COMMAND(IDC_STATIC, &CSeclearDlg::OnStatic)
	ON_WM_CLOSE() 
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BUTTON3, &CSeclearDlg::OnBnClickedButton3)
END_MESSAGE_MAP()


// CSeclearDlg 消息处理程序

BOOL CSeclearDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码  

	m_font.CreatePointFont(100, _T("宋体"), NULL);
	m_show_tis.SetFont(&m_font,true);

	wxpath = setPath();
	
	this->SetWindowTextA(wxpath);
	
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CSeclearDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CSeclearDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

 
UINT  ThreadProc(LPVOID lpParam)
{ 
	CSeclearDlg *pDlg = (CSeclearDlg *)lpParam;
	fillZero(pDlg->wxpath.GetBuffer(0), lpParam);
	delZero(pDlg->wxpath.GetBuffer(0), lpParam);
	pDlg->msg("清理结束,感谢您的本次使用！");
	pDlg->setRun(false);
	return 0;
}

/* 查询进程是否存在 */
bool isWxStatus() {
	if (GetProcessidFromName("WeChatApp.exe") > 0)
	{
		return true;
	}
	if (GetProcessidFromName("WeChat.exe") > 0)
	{
		return true;
	}	
	if (GetProcessidFromName("WeChatPlayer.exe") > 0)
	{
		return true;
	}	
	if (GetProcessidFromName("WechatBrowser.exe") > 0)
	{
		return true;
	}
	return false;
}
  
void CSeclearDlg::OnBnClickedButton1()
{
		if (isWxStatus()) {
			AfxMessageBox("存在微信相关进程，可关闭微信或者重启电脑后重试！");
			return;
		}
	if (!wxpath.IsEmpty()) {
		isRun = true;
		AfxBeginThread(ThreadProc,this);
	}
	else {
		AfxMessageBox("没有找到微信文件");
	}
}


void CSeclearDlg::OnStatic()
{
	// TODO: 在此添加命令处理程序代码
}


void CSeclearDlg::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值 
	if (!isRun) {
		CDialog::OnClose();
	}
	else {
		AfxMessageBox("请等待执行结束");
	}
}


int CSeclearDlg::resetStatic()
{ 
	return 0;
}


void CSeclearDlg::drawStaticRect(RECT rc)
{  
}


RECT * CSeclearDlg::getStaticRect(RECT *rc)
{  
	return rc;
}


void CSeclearDlg::OnStnClickedStatic3()
{
	// TODO: 在此添加控件通知处理程序代码
}



HBRUSH CSeclearDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	if (pWnd->GetDlgCtrlID() == show_tip) {
		pDC->SetTextColor(RGB(63, 133, 255));
	}
	// TODO:  在此更改 DC 的任何特性

	// TODO:  如果默认的不是所需画笔，则返回另一个画笔
	return hbr;
}


void CSeclearDlg::msg(CString c)
{
	// TODO: 在此处添加实现代码.
	MessageBox(c, "提示", MB_ICONINFORMATION);
}


void CSeclearDlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!isRun) {
		::SendMessage(AfxGetMainWnd()->m_hWnd, WM_CLOSE, 0, 0);//最常用
		::PostMessage(AfxGetMainWnd()->m_hWnd, WM_CLOSE, 0, 0);//最常用
	}
	else {
		AfxMessageBox("请等待执行结束");
	}

}


void CSeclearDlg::setRun(bool c)
{
	isRun = c;
	// TODO: 在此处添加实现代码.
}
